package com.liuyz.essync.controller;

import com.liuyz.essync.entity.Sync;
import com.liuyz.essync.entity.SyncConfigBean;
import com.liuyz.essync.utils.EsUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/")
public class EsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    EsUtil esUtil;

    @Autowired
    SyncConfigBean syncConfigBean;

    // 首页
    @RequestMapping(value = "/")
    public String index(Model model) {
        List<String> oldIndexNameList = new ArrayList<>();
        for (Sync sync : syncConfigBean.getSyncList()) {
            oldIndexNameList.add(sync.getOldIndexName());
        }
        model.addAttribute("oldIndexNameList", oldIndexNameList);
        return "es-sync";
    }

    // 普通数据同步
    @ResponseBody
    @RequestMapping(value = "/es/sync")
    public Object sync() {
        try {
            esUtil.sync();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            return e.getMessage();
        }
        return "同步完成！";
    }

    // 规则数据同步
    @ResponseBody
    @RequestMapping(value = "/form/sync", method = RequestMethod.POST)
    public String formSync(String sql, String keyWord, String ruler) {
        try {
            if (StringUtils.isAnyBlank(sql, keyWord, ruler)) {
                return "参数校验失败：参数不能为空";
            }
            if (sql.contains("delete") || sql.contains("truncate") || sql.contains("update") || sql.contains("insert")) {
                return "参数校验失败：只能为查询语句";
            }
            if (!sql.toLowerCase().contains("select")) {
                return "参数校验失败：非查询语句";
            }
            if (!sql.contains("\"mapKey\"")) {
                return "参数校验失败：\"mapKey\"不能省略或写为其他内容";
            }
            if (!(ruler.equals("equal") || ruler.equals("like"))) {
                return "参数校验失败：规则选择异常";
            }

            esUtil.formSync(sql, keyWord, ruler);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            return e.getMessage();
        }
        return "同步完成！";
    }

    // 规则数据预览
    @ResponseBody
    @RequestMapping(value = "/form/show", method = RequestMethod.POST)
    public Object formShow(String sql, String keyWord, String ruler) {
        try {
            if (StringUtils.isAnyBlank(sql, keyWord, ruler)) {
                return "参数校验失败：参数不能为空";
            }
            if (sql.contains("delete") || sql.contains("truncate") || sql.contains("update") || sql.contains("insert")) {
                return "参数校验失败：只能为查询语句";
            }
            if (!sql.toLowerCase().contains("select")) {
                return "参数校验失败：非查询语句";
            }
            if (!sql.contains("\"mapKey\"")) {
                return "参数校验失败：\"mapKey\"不能省略或写为其他内容";
            }
            if (!(ruler.equals("equal") || ruler.equals("like"))) {
                return "参数校验失败：规则选择异常";
            }

            return esUtil.formShow(sql, keyWord, ruler);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            return e.getMessage();
        }
    }

}
